package com.bootdo.system.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartFile;

import com.bootdo.system.domain.DealSlideshowDO;
import com.bootdo.system.service.DealSlideshowService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;

import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.OssUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * @ClassName: DealSlideshowController  
 * @Description: TODO(移动端交易中心轮播图)  
 * @author lzy  
 * @date 2018年6月6日  
 *
 */
 
@Controller
@RequestMapping("/system/dealSlideshow")
public class DealSlideshowController {
	@Autowired
	private DealSlideshowService dealSlideshowService;
	
	@Autowired
	private BootdoConfig bootdoConfig;
	
	@GetMapping()
	String DealSlideshow(){
	    return "system/dealSlideshow/dealSlideshow";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@Cacheable(value = "app", key = "'dealSlideshow'")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<DealSlideshowDO> dealSlideshowList = dealSlideshowService.list(query);
		int total = dealSlideshowService.count(query);
		PageUtils pageUtils = new PageUtils(dealSlideshowList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "system/dealSlideshow/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id,Model model){
		DealSlideshowDO dealSlideshow = dealSlideshowService.get(id);
		model.addAttribute("dealSlideshow", dealSlideshow);
	    return "system/dealSlideshow/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@CacheEvict(value = "app", key = "'dealSlideshow'")
	public R save(@RequestParam("file") MultipartFile file,DealSlideshowDO dealSlideshow){
		String URL = null;
		try {
			URL = OssUtil.fileUpload(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		dealSlideshow.setImgUrl(URL);
		dealSlideshow.setAddTime(new Date());
		if(dealSlideshowService.save(dealSlideshow)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:dealSlideshow:edit")
	public R update( DealSlideshowDO dealSlideshow){
		dealSlideshowService.update(dealSlideshow);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@CacheEvict(value = "app", key = "'dealSlideshow'")
	public R remove(Integer id){
		String imgUrl = dealSlideshowService.get(id).getImgUrl().substring(45, 62);
		if (dealSlideshowService.remove(id) > 0) {
			OssUtil.deleteBucketInfo(imgUrl);
			return R.ok();
		} else {
			return R.error();
		
		}
	}
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:dealSlideshow:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		dealSlideshowService.batchRemove(ids);
		return R.ok();
	}
	
}
